package com.example.list.note;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.list.R;
import com.example.list.recyleview.MyAdapter;
import com.google.gson.Gson;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import java.io.File;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;

public class NoteActivity extends AppCompatActivity {
    String documentStr;
    private List<NoteBean.NoteDTO.NoteRecordDTO> ListResponse;
    private RecyclerView rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        ListResponse=new ArrayList<>();
        rl = (RecyclerView) findViewById(R.id.rl);
        //创建SAXReader对象
        SAXReader reader = new SAXReader();
        //读取文件 转换成Document
        Document document = null;
        try {
//            document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));
            document = reader.read(this.getResources().openRawResource(R.raw.oneplus_note));
//            document = reader.read(this.getResources().openRawResource(R.raw.student));
            //document转换为String字符串
            documentStr = document.asXML();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("document 字符串 异常：" + e.toString());
        }
        Log.d("document XML", documentStr);
        System.out.println("document 字符串：" + documentStr);


        JSONObject jsonObj = null;
        if (documentStr != null) {
            try {
                jsonObj = XML.toJSONObject(documentStr);
                Log.d(" document JSON", jsonObj.toString());
                NoteBean bean = new Gson().fromJson(jsonObj.toString(), NoteBean.class);
                ListResponse.addAll(bean.getNote().getNoteRecord());


                final MyAdapterNote adapter = new MyAdapterNote(this, ListResponse);
                final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                rl.setLayoutManager(layoutManager);
                rl.setAdapter(adapter);

            } catch (JSONException e) {
                Log.e("JSON exception", e.getMessage());
                e.printStackTrace();
            }
        }
    }

}